package com.cnswhy.cloud.server.merchant.controller.iot;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.server.core.entity.IotTerminalInfo;
import com.cnswhy.cloud.server.core.dto.IotTerminalInfoSaveDTO;
import com.cnswhy.cloud.server.core.dto.IotTerminalInfoUpdateDTO;
import com.cnswhy.cloud.server.core.dto.IotTerminalInfoPageDTO;
import com.cnswhy.cloud.server.core.service.IotTerminalInfoService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-03-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/iotTerminalInfo")
@Api(value = "IotTerminalInfo", tags = "终端设备管理")
//@PreAuth(replace = "iotTerminalInfo:")
public class IotTerminalInfoController extends SuperController<IotTerminalInfoService, Long, IotTerminalInfo, IotTerminalInfoPageDTO, IotTerminalInfoSaveDTO, IotTerminalInfoUpdateDTO> {
    @Autowired
    CustomerService customerService;
    @Autowired
    MerchantService merchantService;
    @Override
    public void handlerResult(IPage<IotTerminalInfo> page) {
        for(IotTerminalInfo iotTerminalInfo : page.getRecords()){
            iotTerminalInfo.setCustomer(customerService.getById(iotTerminalInfo.getCustomerId()));
            iotTerminalInfo.setMerchant(merchantService.getById(iotTerminalInfo.getMerchantId()));
        }
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<IotTerminalInfo> iotTerminalInfoList = list.stream().map((map) -> {
            IotTerminalInfo iotTerminalInfo = IotTerminalInfo.builder().build();
            return iotTerminalInfo;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(iotTerminalInfoList));
    }
}
